package lumis.content.wizard;

import lumis.portal.PortalException;
import lumis.portal.PortalObjectAlreadyExistsException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.portal.UnexpectedException;
import lumis.portal.dao.DaoException;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.util.ITransaction;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *	DAO class for Content Winzard Service.
 *
 * @version $Revision: 8289 $ $Date: 2007-09-24 17:46:32 -0300 (Mon, 24 Sep 2007) $
 * @since 4.0.11
 */
class ContentWizardServiceDaoJdbc implements IContentWizardServiceDao
{
	public void add(ContentWizardServiceConfig wizardServiceConfig, ITransaction transaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("insert into lum_ContentWizardService (id, serviceId) values (?, ?) ");
			try
			{
				statement.setString(1, wizardServiceConfig.getId());
				statement.setString(2, wizardServiceConfig.getServiceId());

				try
				{
					statement.executeUpdate();
				}
				catch (Exception e) 
				{
					try
					{
						if(get(wizardServiceConfig.getId(), transaction) != null)
							throw new PortalObjectAlreadyExistsException("STR_RESOURCE_ALREADY_EXISTS;" + wizardServiceConfig.getId(), e);
						else
							throw e;
					}
					catch (PortalObjectNotFoundException e1)
					{
						throw e;
					}
				}
			}
			finally
			{
				statement.close();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public ContentWizardServiceConfig findByServiceId(String serviceId, ITransaction transaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
			
			ContentWizardServiceConfig contentWizardServiceConfig = null;
			
			PreparedStatement statement = connection.prepareStatement("select id, serviceId from lum_ContentWizardService where serviceId = ?");
			try
			{
				statement.setString(1, serviceId);
				
				ResultSet resultSet = statement.executeQuery();
				try
				{
					if (resultSet.next())
					{
						contentWizardServiceConfig = new ContentWizardServiceConfig();
						
						contentWizardServiceConfig.setId(resultSet.getString("id"));
						contentWizardServiceConfig.setServiceId(resultSet.getString("serviceId"));
					}
				}
				finally
				{
					resultSet.close();
				}
			}
			finally
			{
				statement.close();
			}
			
			return contentWizardServiceConfig;
		}
		catch (SQLException e)
		{
			throw new UnexpectedException(e);
		}
	}
	
	public ContentWizardServiceConfig get(String id, ITransaction transaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			ContentWizardServiceConfig contentWizardServiceConfig = null;

			PreparedStatement statement = connection.prepareStatement("select id, serviceId from lum_ContentWizardService where id = ?");
			try
			{
				statement.setString(1, id);

				ResultSet resultSet = statement.executeQuery();
				try
				{
					if (resultSet.next())
					{
						contentWizardServiceConfig = new ContentWizardServiceConfig();

						contentWizardServiceConfig.setId(resultSet.getString("id"));
						contentWizardServiceConfig.setServiceId(resultSet.getString("serviceId"));
					}
				}
				finally
				{
					resultSet.close();
				}
			}
			finally
			{
				statement.close();
			}

			return contentWizardServiceConfig;
		}
		catch (SQLException e)
		{
			throw new UnexpectedException(e);
		}
	}

	public void delete(String id, ITransaction transaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("delete from lum_ContentWizardService where id = ?");
			try
			{
				statement.setString(1, id);
				statement.executeUpdate();
			}
			finally
			{
				statement.close();
			}
		}
		catch (SQLException e)
		{
			throw new UnexpectedException(e);
		}
	}
}
